package com.updesignsz.admin.configuration;


import com.updesignsz.admin.util.ResponseUtil;
import com.updesignsz.commom.util.ResultVo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;

/**
 * 应用异常处理器.
 *
 * @author Lei
 * @since 1.0
 */
@ControllerAdvice
public class ApplicationExceptionHandler {


//    /**
//     * 入参校验异常.
//     */
//    @ExceptionHandler(ConstraintViolationException.class)
//    @ResponseBody
//    public RestResult handleConstraintViolationException(ConstraintViolationException e) {
//        List<ConstraintViolation<?>> violationList = new ArrayList<>(e.getConstraintViolations());
//
//        ConstraintViolation<?> violation = violationList.get(0);
//        return ResultBuilder.failure(1002, violation.getMessage()).build();
//    }

    /**
     * 入参对象校验异常.
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public void handleMethodArgumentNotValidException(HttpServletResponse response, MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        FieldError fieldError = bindingResult.getFieldError();
        ResultVo vo = new ResultVo();
        vo.setResultVo(1, fieldError.getDefaultMessage());
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }


}
